import http from './http'

/**
 * User preferences interface
 */
export interface UserPreferences {
  theme: 'light' | 'dark' | 'auto'
  language: 'en' | 'zh' | 'auto'
  timezone: string
  dateFormat: string
  timeFormat: '12h' | '24h'
  notifications: {
    email: boolean
    browser: boolean
    sound: boolean
  }
  dashboard: {
    layout: string[]
    defaultView: string
  }
}

/**
 * System config interface
 */
export interface SystemConfig {
  siteName: string
  siteUrl: string
  adminEmail: string
  alertRetentionDays: number
  ticketRetentionDays: number
  enableAIAnalysis: boolean
  enableAutoCreateTicket: boolean
  slaEnabled: boolean
  slaPolicies: {
    critical: number
    high: number
    medium: number
    low: number
  }
  maintenanceMode: boolean
  maintenanceMessage: string
}

/**
 * Integration config interface
 */
export interface IntegrationConfig {
  prometheus?: {
    url: string
    enabled: boolean
  }
  grafana?: {
    url: string
    enabled: boolean
  }
  alertmanager?: {
    url: string
    enabled: boolean
  }
  openai?: {
    apiKey: string
    model: string
    enabled: boolean
  }
}

/**
 * Notification channel interface
 */
export interface NotificationChannel {
  type: 'email' | 'slack' | 'dingtalk' | 'webhook'
  enabled: boolean
  config: {
    [key: string]: any
  }
}

// ============= User Settings =============

/**
 * Get current user's settings
 */
export const getUserSettings = async () => {
  return await http.get('/settings/user')
}

/**
 * Get current user's preferences
 */
export const getUserPreferences = async () => {
  return await http.get<UserPreferences>('/settings/preferences')
}

/**
 * Update current user's preferences
 */
export const updateUserPreferences = async (preferences: Partial<UserPreferences>) => {
  return await http.put<UserPreferences>('/settings/preferences', preferences)
}

/**
 * Update user setting
 */
export const updateUserSetting = async (key: string, value: any) => {
  return await http.put('/settings/user', { key, value })
}

/**
 * Reset user settings to defaults
 */
export const resetUserSettings = async () => {
  return await http.delete('/settings/user')
}

// ============= System Settings =============

/**
 * Get system settings
 */
export const getSystemSettings = async () => {
  return await http.get('/settings/system')
}

/**
 * Get system config
 */
export const getSystemConfig = async () => {
  return await http.get<SystemConfig>('/settings/system/config')
}

/**
 * Update system config
 */
export const updateSystemConfig = async (config: Partial<SystemConfig>) => {
  return await http.put<SystemConfig>('/settings/system/config', config)
}

/**
 * Update system setting
 */
export const updateSystemSetting = async (key: string, value: any) => {
  return await http.put('/settings/system', { key, value })
}

/**
 * Reset system settings to defaults
 */
export const resetSystemSettings = async () => {
  return await http.delete('/settings/system')
}

// ============= Integration Settings =============

/**
 * Get integration settings
 */
export const getIntegrationSettings = async () => {
  return await http.get('/settings/integrations')
}

/**
 * Get integration config
 */
export const getIntegrationConfig = async () => {
  return await http.get<IntegrationConfig>('/settings/integrations/config')
}

/**
 * Update integration setting
 */
export const updateIntegrationSetting = async (key: string, value: any) => {
  return await http.put('/settings/integrations', { key, value })
}

// ============= Notification Settings =============

/**
 * Get notification channels
 */
export const getNotificationChannels = async () => {
  return await http.get<NotificationChannel[]>('/settings/notifications/channels')
}

/**
 * Update notification channels
 */
export const updateNotificationChannels = async (channels: NotificationChannel[]) => {
  return await http.put<NotificationChannel[]>('/settings/notifications/channels', channels)
}
